<?php

namespace Frieza\Modules\Live\Controllers;

use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Tab;
use Illuminate\Http\Request;
use Encore\Admin\Widgets\Form;
use Illuminate\Support\MessageBag;
use Doraemon\model\Member;
use Doraemon\service\Live\LiveLevel as sLiveLevel;
use Doraemon\model\Live\LiveAnchorLevel as mLiveAnchorLevel;

class AnchorLevelController extends Controller
{
    const PAGE_SIZE = 20;

    public function __construct()
    {
        parent::__construct();
        Admin::script('$(".pjax-container").attr("id","pjax-container");');
    }

    public function index(Request $request, Content $content)
    {
        Admin::script($this->setScript());
        $pageSize = $request->input("per_page", 20);
        $page = $request->input("page", 1);
        $where= [];
        if($request->input('nick')){
            $where['nick~'] = $request->input('nick');
        }
        $where['LIMIT'] = [
            ($page - 1) * $pageSize,
            $pageSize
        ];
        $list = mLiveAnchorLevel::selectByWhere($where);

        $result['list'] = $list;
        unset($where['LIMIT']);
        $result['total'] = mLiveAnchorLevel::selectCount($where);
        $gridModel = new pGridDataModel($result['list'], $result['total'], [
            'perPage' => $pageSize
        ]);

        $gridList = Admin::grid($gridModel,
            function (Grid $grid) use ($gridModel) {
                $grid->paginate($gridModel->getPerPage());
                $grid->filter(function (Grid\Filter $filter) {
                    // 去掉默认的id过滤器
                    $filter->disableIdFilter();
                    $filter->column(1 / 2, function ($filter) {
                        $filter->equal('nick', '昵称');
                    });
                    if (!empty($condition)) {
                        $filter->expand();//默认展开
                    }
                });
                $grid->disableExport();
                $grid->disableRowSelector();
                $grid->column('uid', 'uid')->width(40);
                $grid->column('nick', '用户昵称')->width(80);
                $grid->column('level', '等级')->width(100)->editable();
                $grid->column('index', '指数')->width(200)->display(function (){
                     $display = '';
                     $display.='<p>uv : '.$this->uv_index."</p>";
                     $display.='<p>点赞 : '.$this->like_index."</p>";
                     $display.='<p>粉丝 : '.$this->fans_index."</p>";
                     $display.='<p>时长 : '.$this->ts_index."</p>";
                     $display.='<p>评论 : '.$this->comment_index."</p>";
                    $display.='<p>场次 : '.$this->cnt."</p>";

                    return $display;
                });
                $grid->disableActions();
                $grid->disableCreateButton();
            }
        );
        return $content->header('主播等级')->breadcrumb(
            ['text' => '主播等级'])
            ->row($gridList->render());
    }


    public function changeLevel(Request $request, Content $content , $id)
    {
        $name = $request->input('name', '');
        $value = $request->input('value', 0);
        $params = $request->all();
        try {
            $info = mLiveAnchorLevel::getByWhere(['id' => $id]);
            if ($info && $name == 'level' && $value) {
                mLiveAnchorLevel::updateData(['level' => $value], ['id' => $id]);
                return response()->json([
                    'status' => true,
                ]);
            }else {
                throw new \Exception('参数错误或则不存在的直播');
            }
        } catch (\Exception $e) {
            return response()->json(['errors' => [$message = $e->getMessage()]], 422);
        }
    }

    /*设置scirpt
    *
    * */
    private function setScript()
    {
        return <<<SCRIPT
       
        //删除
         $('.delcontent').unbind('click').click(function(){
            var id= $(this).data('id');

            
            layer.confirm('确定要删除么？', {
                btn: ['确定', '取消']
            }, function (index) {
                layer.close(index);
                 let url = '/admin/live/index/backList/delContent/'+ id;
                let params = {};
                $.post(url, params , function (result) {
                    if (result.status == 1) {
                        layer.msg(result.message);
                        setTimeout(function () {
                            parent.window.location.reload();
                        }, 2000);
                    } else {
                        layer.msg(result.message);
                    }
                });
            });
        });
SCRIPT;
    }


}